﻿using MiniExcelLibs;

namespace Zhp.Common.Helper
{
    public static class ExcelHelper
    {
        public static async Task<IEnumerable<T>> Import<T>(Stream stream) where T : class, new()
        {
            return await MiniExcel.QueryAsync<T>(stream);
        }

        public static async Task<(Stream, string, string)> Export(object value, string fileName = null)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                fileName = $"{DateTime.Now:yyyyMMddHHmmss}.xlsx";
            }
            // 创建内存流
            var stream = new MemoryStream();
            await stream.SaveAsAsync(value);
            // 设置流中的读取位置
            stream.Seek(0, SeekOrigin.Begin);
            return (stream, MimeTypeHelper.TextXlsx, fileName);
        }

    }
}
